package com.zxl.mytest11;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 占位符参数解析
 * @author zxl
 *
 */
public class TempTest {
    
    public static void main(String[] args) throws Exception {
//        String str = "您的金鹏账户卡号已被合并，原卡号{adwa}已合并入卡号{weg},getesdaw{ner}";
//        System.out.println(replaceParam(str));
        
//        String str = "您的金鹏账户卡号已被合并，原卡号{adwa}已合并入卡号{weg},getesdaw{$ner}";
//        Map<String, Object> map = new HashMap<>();
//        map.put("awf", "b");
//        map.put("we$g", "e");
//        map.put("ber", "d");
//        map.put("wer", "a");
//        map.put("$ner", "中文");
//        test((String) map.get("adw"));
//        System.out.println(map.get(""));
        
//        String str = "您的金鹏账户卡号已被合并，原卡号{name}已合并入卡号{age},getesdaw{gender}";
//        Person person = new Person();
//        person.setAge(12);
//        person.setName("abc");
//        person.setGender(null);
//        System.out.println(replaceParam(str, person));
        TreeMap<Long, String> temp = new TreeMap<>();
        List<String> times = new ArrayList<>();
        times.add("2017-09-25 12:45");
        times.add("2017-09-25 06:40");
        times.add("2017-09-25 06:50");
        times.add("2017-09-25 23:50");
        times.add("2017-09-25 23:55");
        try {
            SimpleDateFormat oSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            for (String time : times) {
                Date parse = oSimpleDateFormat.parse(time);
                long mils = parse.getTime();
                temp.put(mils, time + " === " + mils + " === " + parse);
            }
        } catch (Exception oException) {
            System.out.println(oException.getMessage());
        }
        
        for (Long key : temp.keySet()) {
            System.out.println(temp.get(key));
        }
        System.out.println();
        for (String str : temp.values()) {
            System.out.println(str);
        }
//        int a = 0;
//        int b = 1;
//        int c = -1;
//        System.out.println(-a);
//        System.out.println(-b);
//        System.out.println(-c);
//        map.put("123", true);
//        Object temp = map.get("123");
//        double discount = Double.parseDouble("8") * 10;
//        NumberFormat numberFormat = NumberFormat.getInstance();
//        numberFormat.format(discount);
//        System.out.println(numberFormat.format(discount));
        
//        String [] abc = {"a","b"};
//        String [] abc2 = {"c","d"};
//        String[] sss = new String[abc.length + abc2.length];
//        System.arraycopy(abc, 0, sss, 0, abc.length);
//        System.arraycopy(abc2, 0, sss, abc.length, abc2.length);
//        for (String str : sss) {
//            System.out.println(str);
//        }
        URL url = new URL("https://localhost:8080/pkpass");
        System.out.println(url);
    }
    
    public static void test(String abc){
        System.out.println(abc);
    }
    
    public static String replaceParam(String str, Map<String, String> paramMap){
        if (str == null || paramMap == null || paramMap.isEmpty()) {
            return str;
        }
        Pattern pattern = Pattern.compile("\\{([\\w$]+)\\}", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            String value = paramMap.get(matcher.group(1));
            if (value != null) {
                matcher.appendReplacement(sb, value);
            }
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
    
    public static String replaceParam(String str, Object paramObj){
        if (str == null || paramObj == null ) {
            return str;
        }
        Pattern pattern = Pattern.compile("\\{([a-zA-Z_$]+[a-zA-Z_$\\d]*)\\}", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        Class<? extends Object> cls = paramObj.getClass();
        String group = "";
        char oldChar = 'a';
        Method method = null;
        Object value = null;
        try {
            while (matcher.find()) {
                group = matcher.group(1);
                oldChar = group.charAt(0);
                method = cls.getMethod("get" + group.replace(oldChar, Character.toUpperCase(oldChar)));
                value = method.invoke(paramObj);
                if (value != null) {
                    matcher.appendReplacement(sb, value.toString());
                }
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
    
    private static class MyComparator implements Comparator<Long>{

        @Override
        public int compare(Long o1, Long o2) {
            //降序排列
//            System.out.println("o1: " + o1 + " o2: " + o2 + " o2 - o1 = " + (o2 - o1));
            return o2 == o1 ? 0 : o2 < o1 ? 1 : -1;
//            return (int) (o2 - o1);
        }
        
    }
}
